package system;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @author Kevin Peters
 */
public class SystemProperties {

	private Properties properties;
	private ResourceBundle messages = null;
	public static String APPTITLE;
	public static int BASICSCALING;
	public static String DEFAULTPICTURENAME;
	public static String COLORCHOOSERTITLE;
	public static String RESOURCEPATH;
	public static int CANVASWIDTH;
	public static int CANVASHEIGHT;
	public static int CANVASCOLS;
	public static int CANVASROWS;
	public static int CANVASHGAP;
	public static int CANVASVGAP;
	public static int GRIDPANELSIDELENGTH;
	public static int RESOURCECOLS;
	public static int RESOURCEROWS;
	public static List<Integer> FILLEDSOURCEPANELS = new LinkedList<Integer>();
	public static List<Integer> FILLEDEDITORPANELS = new LinkedList<Integer>();
	public static Map<Integer, Integer> PANELMAPPING_EDITOR2SOURCE = new HashMap<Integer, Integer>();
	public static Map<Integer, Integer> PANELMAPPING_SOURCE2EDITOR = new HashMap<Integer, Integer>();
	public static String LOADDEFAULTIMAGE;
	public static String LOADCUSTOMIMAGE;
	public static String UNDO;
	public static String REDO;
	public static String SAVE;
	public static String LOADDEFAULTIMAGE_LABEL;
	public static String LOADCUSTOMIMAGE_LABEL;
	public static String UNDO_LABEL;
	public static String REDO_LABEL;
	public static String SAVE_LABEL;
	public static String TOOLTIP_KEY_PREFIX;
	public static String USER_LANGUAGE_KEY;
	public static String USER_COUNTRY_KEY;
	public static String DEFAULT_LANGUAGE;
	public static String DEFAULT_COUNTRY;
	public static String I18N_BUNDLE;
	public static List<String> AVAILABLE_LANGUAGES = new LinkedList<String>();

	private static String filledSourcePanelString;
	private static String filledSourcePanelArray[];
	private static String filledEditorPanelString;
	private static String filledEditorPanelArray[];
	private static String availableLanguagesString;
	private static String availableLanguagesArray[];
	public static String CONFIRM_LOADING_DEFAULT_IMAGE;
	public static String CONFIRM_LOADING_CUSTOM_IMAGE;
	public static String DEFAULT_FILE_PATH;
	public static String FILE_PATH_DESCRIPTION;
	public static String FILE_EXTENSION;
	public static String FILE_PROTOCOL_PREFIX;
	public static int NEW_IMAGE_WIDTH;
	public static int NEW_IMAGE_HEIGTH;

	/**
	 * Fills the constants with the proper data
	 */
	public void convertDataIntoTypes() {
		APPTITLE = properties.getProperty("appTitle");
		BASICSCALING = Integer.valueOf(properties.getProperty("basicScaling"));
		DEFAULTPICTURENAME = properties.getProperty("defaultPictureName");

		RESOURCEPATH = properties.getProperty("resourcePath");
		CANVASWIDTH = Integer.valueOf(properties.getProperty("canvasWidth"));
		CANVASHEIGHT = Integer.valueOf(properties.getProperty("canvasHeight"));
		CANVASCOLS = Integer.valueOf(properties.getProperty("canvasCols"));
		CANVASROWS = Integer.valueOf(properties.getProperty("canvasRows"));
		CANVASHGAP = Integer.valueOf(properties.getProperty("canvasHgap"));
		CANVASVGAP = Integer.valueOf(properties.getProperty("canvasVgap"));
		GRIDPANELSIDELENGTH = Integer.valueOf(properties
				.getProperty("gridPanelSideLength"));
		RESOURCECOLS = Integer.valueOf(properties.getProperty("resourceCols"));
		RESOURCEROWS = Integer.valueOf(properties.getProperty("resourceRows"));
		LOADDEFAULTIMAGE = properties.getProperty("loadDefaultImageCommand");
		LOADCUSTOMIMAGE = properties.getProperty("loadCustomImageCommand");
		SAVE = properties.getProperty("saveCommand");
		TOOLTIP_KEY_PREFIX = properties.getProperty("toolTipKeyPrefix");
		USER_LANGUAGE_KEY = properties.getProperty("userLanguageKey");
		USER_COUNTRY_KEY = properties.getProperty("userCountryKey");
		DEFAULT_LANGUAGE = properties.getProperty("defaultLanguage");
		DEFAULT_COUNTRY = properties.getProperty("defaultCountry");
		I18N_BUNDLE = properties.getProperty("i18nBundle");
		DEFAULT_FILE_PATH = properties.getProperty("defaultFilePath");
		FILE_EXTENSION = properties.getProperty("fileExtension");
		NEW_IMAGE_WIDTH = Integer.valueOf(properties.getProperty("newImageWidth"));
		NEW_IMAGE_HEIGTH = Integer.valueOf(properties.getProperty("newImageHeight"));
		FILE_PROTOCOL_PREFIX = properties.getProperty("fileProtocolPrefix");
		UNDO = properties.getProperty("undoCommand");
		REDO = properties.getProperty("redoCommand");

		filledSourcePanelString = properties.getProperty("filledSourcePanels");
		filledSourcePanelArray = filledSourcePanelString.split(",");
		filledEditorPanelString = properties.getProperty("filledEditorPanels");
		filledEditorPanelArray = filledEditorPanelString.split(",");
		availableLanguagesString = properties.getProperty("availableLanguages");
		availableLanguagesArray = availableLanguagesString.split(",");

		for (int x = 0; x < filledSourcePanelArray.length; x++) {
			Integer sourceInt = Integer.valueOf(filledSourcePanelArray[x]);
			Integer editorInt = Integer.valueOf(filledEditorPanelArray[x]);
			FILLEDSOURCEPANELS.add(sourceInt);
			FILLEDEDITORPANELS.add(editorInt);
			PANELMAPPING_EDITOR2SOURCE.put(editorInt, sourceInt);
			PANELMAPPING_SOURCE2EDITOR.put(sourceInt, editorInt);
		}
		
		for (int x = 0; x < availableLanguagesArray.length; x++) {
			String language = availableLanguagesArray[x];
			AVAILABLE_LANGUAGES.add(language);
		}
	}

	/**
	 * Reads the properties file and initializes the Properties object
	 * 
	 * @return true if successfully loaded, false if not
	 */
	public boolean readPropertiesFile() {
		properties = new Properties();
		try {
			properties.load(this.getClass().getResourceAsStream(
					"/resources/configuration/semApp.properties"));
		} catch (IOException e) {
			System.out.println("Could not load properties file!");
			return false;
		}

		return true;
	}

	/**
	 * Fills the gui text elements with localized strings
	 */
	public void readLocalTranslations() {

		String userLanguage = System
				.getProperty(SystemProperties.USER_LANGUAGE_KEY);
		String userCountry = System
				.getProperty(SystemProperties.USER_COUNTRY_KEY);
		Locale currentLocale = null;
		
		

		if (userLanguage != null && userCountry != null && AVAILABLE_LANGUAGES.contains(userLanguage + "_" + userCountry)){
			currentLocale = new Locale(userLanguage,
					userCountry);
		} else {
			currentLocale = new Locale(SystemProperties.DEFAULT_LANGUAGE,
					SystemProperties.DEFAULT_COUNTRY);
		}
		
		messages = ResourceBundle.getBundle(SystemProperties.I18N_BUNDLE,
				currentLocale);

		COLORCHOOSERTITLE = messages.getString("color_choser_title");
		LOADDEFAULTIMAGE_LABEL = messages.getString("load_default_image");
		LOADCUSTOMIMAGE_LABEL = messages.getString("load_custom_image");
		SAVE_LABEL = messages.getString("save");
		CONFIRM_LOADING_DEFAULT_IMAGE = messages.getString("confirmLoadingDefaultImage");
		CONFIRM_LOADING_CUSTOM_IMAGE = messages.getString("confirmLoadingCustomImage");
		FILE_PATH_DESCRIPTION = messages.getString("fileDescription");
		UNDO_LABEL = messages.getString("undo");
		REDO_LABEL = messages.getString("redo");
	}

	/**
	 * Returns the proper tool tip text for editor panel with the given index
	 * 
	 * @param editorPanelIndex
	 * @return Tool tip text
	 */
	public String getToolTipText(int editorPanelIndex) {
		String toolTipText = "";
		String toolTipKeyPrefix = SystemProperties.TOOLTIP_KEY_PREFIX;
		String i18nKey = properties.getProperty(toolTipKeyPrefix
				+ editorPanelIndex);
		toolTipText = messages.getString(i18nKey);
		return toolTipText;
	}

}
